<!DOCTYPE html>
<html>
<head>
<title>RC Circuit Diagram</title>
</head>
<body>
<canvas id="circuitCanvas" width="400" height="320"></canvas>
<script>
    const canvas = document.getElementById('circuitCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.lineWidth = 1.5;
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.font = '20px Arial';
    ctx.textAlign = 'center';

    // --- Coordinates ---
    const x1 = 50, y1 = 60;
    const x2 = 350, y2 = 210;
    const centerX = (x1 + x2) / 2;
    const width = x2 - x1;

    // --- Draw Components ---
    
    // Capacitor (C)
    const capX = x1 + width * 0.25;
    const capGap = 12;
    const plateHeight = 15;
    ctx.beginPath();
    ctx.moveTo(capX - capGap / 2, y1 - plateHeight);
    ctx.lineTo(capX - capGap / 2, y1 + plateHeight);
    ctx.moveTo(capX + capGap / 2, y1 - plateHeight);
    ctx.lineTo(capX + capGap / 2, y1 + plateHeight);
    ctx.stroke();
    ctx.textBaseline = 'bottom';
    ctx.fillText('C', capX, y1 - plateHeight - 5);

    // Resistor (R)
    const resX = x1 + width * 0.75;
    const resWidth = 50;
    const resHeight = 12;
    ctx.strokeRect(resX - resWidth / 2, y1 - resHeight / 2, resWidth, resHeight);
    ctx.textBaseline = 'bottom';
    ctx.fillText('R', resX, y1 - resHeight / 2 - 8);

    // Power Source (ε)
    const emfX = x1 + width * 0.25;
    const emfGap = 20;
    const terminalRadius = 3;
    ctx.beginPath();
    ctx.arc(emfX - emfGap / 2, y2, terminalRadius, 0, 2 * Math.PI);
    ctx.moveTo(emfX + emfGap / 2 + terminalRadius, y2); // move to start next arc
    ctx.arc(emfX + emfGap / 2, y2, terminalRadius, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.font = '22px "Times New Roman"';
    ctx.textBaseline = 'middle';
    ctx.fillText('ε', emfX, y2);
    ctx.font = '20px Arial'; // Reset font

    // Switch (K)
    const switchX = x1 + width * 0.75;
    const switchGap = 30;
    const switchTerminalRadius = 3.5;
    ctx.beginPath();
    ctx.arc(switchX - switchGap / 2, y2, switchTerminalRadius, 0, 2 * Math.PI);
    ctx.stroke(); // Draw left terminal
    ctx.beginPath();
    ctx.arc(switchX + switchGap / 2, y2, switchTerminalRadius, 0, 2 * Math.PI);
    ctx.stroke(); // Draw right terminal
    ctx.beginPath();
    ctx.moveTo(switchX - switchGap / 2, y2);
    ctx.lineTo(switchX + switchGap / 2 - 5, y2 - 20); // Draw switch arm
    ctx.stroke();
    ctx.textBaseline = 'bottom';
    ctx.fillText('K', switchX, y2 - 25);
    
    // --- Draw Wires ---
    ctx.beginPath();
    // Top wire
    ctx.moveTo(x1, y1);
    ctx.lineTo(capX - capGap / 2, y1);
    ctx.moveTo(capX + capGap / 2, y1);
    ctx.lineTo(resX - resWidth / 2, y1);
    ctx.moveTo(resX + resWidth / 2, y1);
    ctx.lineTo(x2, y1);
    
    // Bottom wire
    ctx.moveTo(x1, y2);
    ctx.lineTo(emfX - emfGap / 2, y2);
    ctx.moveTo(emfX + emfGap / 2, y2);
    ctx.lineTo(switchX - switchGap / 2, y2);
    ctx.moveTo(switchX + switchGap / 2, y2);
    ctx.lineTo(x2, y2);

    // Vertical wires
    ctx.moveTo(x1, y1);
    ctx.lineTo(x1, y2);
    ctx.moveTo(x2, y1);
    ctx.lineTo(x2, y2);
    ctx.stroke();

    // --- Figure Label ---
    ctx.font = '24px "KaiTi", "SimSun"';
    ctx.textBaseline = 'top';
    ctx.fillText('图 1', centerX, y2 + 30);

</script>
</body>
</html>